package code4;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServerThread extends Thread {

    private Socket s;

    public ServerThread(Socket s){this.s=s;}

    @Override
    public void run(){
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {

            ois = new ObjectInputStream(s.getInputStream());
            oos = new ObjectOutputStream(s.getOutputStream());

            while(true){
                UserMessage obj = (UserMessage)ois.readObject();
                if("admin".equals(obj.getUser().getUserName()) && "123456".equals(obj.getUser().getPassage())){
                    obj.setUserClass("success");
                    oos.writeObject(obj);
                    System.out.println("服务器发送数据成功！");
                    break;
                }else{
                    obj.setUserClass("fail");
                    oos.writeObject(obj);
                    System.out.println("服务器发送数据成功！");
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(null!=ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=s){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
